package com.weitu.majorHazard.statisticsAnalysis.service.impl;

import com.weitu.majorHazard.statisticsAnalysis.entity.ActivityRecordStatisticsAnalysis;
import com.weitu.majorHazard.statisticsAnalysis.entity.DeviceLevelStatisticsAnalysis;
import com.weitu.majorHazard.statisticsAnalysis.mapper.StatisticsAnalysisMapper;
import com.weitu.majorHazard.statisticsAnalysis.service.StatisticsAnalysisService;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by smp on 2017/4/24.
 */
@Service
public class StatisticsAnalysisServiceImpl implements StatisticsAnalysisService {
    private final StatisticsAnalysisMapper statisticsAnalysisMapper;
    private final DepartmentMapper departmentMapper;
    @Autowired
    public StatisticsAnalysisServiceImpl(StatisticsAnalysisMapper statisticsAnalysisMapper,DepartmentMapper departmentMapper){
        this.statisticsAnalysisMapper=statisticsAnalysisMapper;
        this.departmentMapper=departmentMapper;
    }

    private Map<String,Object> objToMap(DeviceLevelStatisticsAnalysis analysis){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",analysis.getId());
        map.put("name",analysis.getName());
        map.put("parentId",analysis.getParentId());
        if(null==analysis.getLevel()){
            map.put("level1",0);
            map.put("level2",0);
            map.put("level3",0);
        }else if(analysis.getLevel().equals("一级")){
            map.put("level1",analysis.getNum());
            map.put("level2",0);
            map.put("level3",0);
        }else if(analysis.getLevel().equals("二级")){
            map.put("level2",analysis.getNum());
            map.put("level1",0);
            map.put("level3",0);
        }else if(analysis.getLevel().equals("三级")){
            map.put("level3",analysis.getNum());
            map.put("level1",0);
            map.put("level2",0);
        }else {
            map.put("level1",0);
            map.put("level2",0);
            map.put("level3",0);
        }
        return map;
    }

    private void combine(List<Map<String,Object>> mapList,Map<String,Object> map){
        boolean flag=true;
        for(int i=0;i<mapList.size();i++){
            if(mapList.get(i).get("id").equals(map.get("id"))){
                mapList.get(i).put("level1",(Integer)(mapList.get(i).get("level1"))+(Integer) (map.get("level1")));
                mapList.get(i).put("level2",(Integer)(mapList.get(i).get("level2"))+(Integer) (map.get("level2")));
                mapList.get(i).put("level3",(Integer)(mapList.get(i).get("level3"))+(Integer) (map.get("level3")));
                flag=false;
            }
        }
        if(flag){
            mapList.add(map);
        }
    }

    @Override
    public List<Map<String,Object>> analysisByLevel(Map<String, Object> map) {
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId("0");
        List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();
        map.put("deptId",parentDepartmentList.get(0).getId());


        List<DeviceLevelStatisticsAnalysis> analysisList=this.statisticsAnalysisMapper.analysisByLevel(map);
        for(int i=0;i<analysisList.size();i++){
           combine(mapList,objToMap(analysisList.get(i)));
        }
        return mapList;
    }


    @Override
    public ActivityRecordStatisticsAnalysis analysisByActivityRecord(Map<String, Object> map) {
        return this.statisticsAnalysisMapper.analysisByActivityRecord(map);
    }


    @Override
    public int checkUserName(String name) {
        return this.statisticsAnalysisMapper.checkUserName(name);
    }
}
